package org.glorylight.hacks.social.controller;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.glorylight.hacks.social.data.TipsData;
import org.glorylight.hacks.social.data.TipsWrapper;
import org.glorylight.hacks.social.data.UserData;
import org.glorylight.hacks.social.data.UserInformation;
import org.glorylight.hacks.social.service.TipsService;
import org.glorylight.hacks.social.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;


@Component
@Scope("prototype")
@Path("/authenticate")
public class LoginController extends AbstractBaseController {
	
	private static final Logger log = LoggerFactory.getLogger(LoginController.class);
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private TipsService tipsService;
	
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public UserInformation authenticate() {
		log.debug("trying to authenticate user");
		UserData userData = userService.find(getActiveLogin());
		List<org.glorylight.hacks.social.model.User> following = userService.findUserToConnection(getActiveLogin());
		List<org.glorylight.hacks.social.model.User> followers = userService.findConnectionToUser(getActiveLogin());
		if(userData == null) return null;
		List<TipsData> tipsDatas = tipsService.findTipsData(userData.getBabyAge());
		return populateUserInformation(userData, new TipsWrapper(tipsDatas), ""+followers.size(), ""+following.size());
	}
	
	private UserInformation populateUserInformation(UserData userData, TipsWrapper tipsWrapper, String followersSize, String followingSize) {
		UserInformation userInformation = new UserInformation(userData.getEmailAddress(), userData.getFirstName(), userData.getLastName(), userData.getBabyName(), ""+userData.getBabyAge(), tipsWrapper, followersSize, followingSize, userData.isActivated().toString());
		userInformation.setUserId(userData.getUserId());
		return userInformation;
	}
}
